<template>
  <div class="assignPeople">
    <Form inline :label-width="90" v-if="manageType == 'userGroup'">
      <Form-item :label="$t('module.userManage.userGroupId')">
        <Input v-model="userGroupInf.id"  disabled />
      </Form-item>
      <Form-item :label="$t('module.userManage.userGroupName')">
        <Input v-model="userGroupInf.name"  disabled />
      </Form-item>
    </Form>
    <Form inline :label-width="90" v-if="manageType == 'role'">
      <Form-item :label="$t('module.userManage.roleId')">
        <Input v-model="userGroupInf.id"  disabled />
      </Form-item>
      <Form-item :label="$t('module.userManage.roleName')">
        <Input v-model="userGroupInf.name"  disabled />
      </Form-item>
    </Form>
    <p class="assign-user">{{this.$t('module.userManage.assignPerson')}}</p>
    <div class="transfer-content">
      <!-- 左侧区域 -->
      <div class="transfer-left tansDiv">
        <div class="left-content">
          <InnerPageTable ref="transferLeftListTable" :config="configLeft">
            <Form
              :model="conditionLeft"
              ref="transferLeftListForm"
              slot="search-bar"
              label-position="top"
              class="ivu-form-no-margin-bottom"
              inline
              :label-width="0"
            >
              <Form-item prop="userName">
                <Input
                  icon="ios-search"
                  :placeholder="$t('common.searchN')"
                  @input="searchHandler('left')"
                  v-model="conditionLeft.userName"
                />
              </Form-item>
            </Form>
          </InnerPageTable>
        </div>
      </div>

      <!-- 添加用户、移除用户 中间穿梭按钮 -->
      <div class="transfer-center tansDiv">
        <div>
          <div class="addUserBtn" @click="addSelectUsersBtn">
            {{$t('module.userManage.addUser')}}
            <span>&gt;&gt;</span>
          </div>
          <div class="deleUserBtn" @click="deletSelectUsersBtn">
            <span>&lt;&lt;</span>
            {{$t('module.userManage.delUser')}}
          </div>
        </div>
      </div>

      <!-- 右侧区域 -->
      <div class="transfer-right tansDiv">
        <div class="right-content">
          <InnerPageTable ref="transferRightListTable" :config="configRight"></InnerPageTable>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { assignUserColumns, userColumns } from './columns'
import { getUserList } from '@api/flowable/readPass.js'
import { addUsersToGroup, userGroupPerson, delUserGroupPeople } from '@api/flowable/userManageAPI'
export default {
  name: 'assignPeople',
  props: {
    userGroupInf: {
      type: Object,
      default() {
        return {}
      }
    },
    manageType: {
      type: String
    }
  },
  data() {
    return {
      conditionLeft: {
        userName: ''
      },
      conditionRight: {
        userName: ''
      },
      configLeft: {
        loadDataFn: ({ responseProcess, param }) => {
          getUserList(param).then(({ data }) => {
            data.currentPage = data.pageNum
            delete data.pageNum
            data.recordCount = data.total
            delete data.total
            data.recordList = data.records
            delete data.records
            responseProcess(data)
          })
        },
        columnsFn: assignUserColumns.bind(this),
        autoFirst: true,
        onSelectionChange: this.setSelectedUsers
      },
      configRight: {
        loadDataFn: ({ responseProcess, param }) => {
          param.groupId = this.userGroupInf.id
          userGroupPerson(param).then(({ data }) => {
            let finalData = {}
            finalData.currentPage = data.data.pageNum
            finalData.recordCount = data.data.total
            finalData.recordList = data.data.list
            responseProcess(finalData)
          })
        },
        columnsFn: userColumns.bind(this),
        autoFirst: true,
        onSelectionChange: this.setDelUsers
      },
      selectedUsers: {},
      delUsers: {}
    }
  },
  methods: {
    //搜索
    searchHandler(searchParam) {
      if (searchParam == 'left') {
        this.$refs['transferLeftListTable'].load(this.conditionLeft, 1)
      }
    },
    //选择要添加的人
    setSelectedUsers(selection) {
      let ids = selection.map(item => item.id)
      this.selectedUsers.id = this.userGroupInf.id
      this.selectedUsers.userIds = ids
    },
    //选择要删除的人
    setDelUsers(users) {
      let ids = users.map(item => item.id)
      this.delUsers.id = this.userGroupInf.id
      this.delUsers.userIds = ids
    },
    addSelectUsersBtn() {
      let uIds = this.selectedUsers.userIds
      if (uIds && uIds.length) {
        addUsersToGroup(this.selectedUsers).then(() => {
          // this.selectedUsers.userIds = []
          this.$Message.success({
            content: this.$t('module.userManage.addSuccess'),
            duration: 3
          })
          this.$refs['transferRightListTable'].load(this.conditionRight, 1)
        })
      } else {
        this.$Message.error({
          content: this.$t('module.userManage.selctAddElement'),
          duration: 3
        })
      }
    },
    deletSelectUsersBtn() {
      let dIds = this.delUsers.userIds
      if (dIds && dIds.length) {
        delUserGroupPeople(this.delUsers).then(() => {
          this.delUsers.userIds = []
          this.$Message.success({
            content: this.$t('module.userManage.delSuccess'),
            duration: 3
          })
          this.$refs['transferRightListTable'].load(this.conditionRight, 1)
        })
      } else {
        this.$Message.error({
          content: this.$t('module.userManage.selctDelElement'),
          duration: 3
        })
      }
    }
  }
}
</script>

<style lang="less">
.assignPeople{
  padding-top: 10px;
  >.ivu-form{
    padding-left: 10px;
  }
  .assign-user {
    padding: 10px 5px;
    font-size: 14px;
    font-weight: 900;
    padding-left: 10px;
    border-bottom: 1px solid rgb(224, 221, 221);
  }
  .transfer-content {
    width: 100%;
    padding: 0 10px;
  }
  .transfer-content {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-top: 16px;
    .tansDiv {
      .single-table-con {
        padding: 0;
      }
      .run-search-bar div {
        margin: 0;
        width: 100%;
      }
      .ivu-form-inline {
        padding: 0;
        min-height: 0;
      }
      .ivu-form-inline .ivu-form-item {
        margin: 0;
      }
      .ivu-form-inline .ivu-input-wrapper {
        width: 100%;
      }
      .ivu-input {
        border-radius: 3px;
      }
      .inner-table {
        min-height: 400px;
      }
      .self-adaption-wrap {
        margin-top: 10px;
      }
      .ivu-table {
        font-size: 12px;
      }
      .table-bar {
        margin: 0 0 10px;
      }
    }
    .transfer-left {
      width: 45%;
      display: flex;
      justify-content: flex-start;
      .left-content {
        width: 100%;
        border: 1px solid rgb(224, 221, 221);
        border-radius: 5px;
        padding: 10px;
      }
    }
    .transfer-center {
      width: 10%;
      position: relative;
      & > div {
        width: 80%;
        position: absolute;
        top: 45%;
        left: 10%;
      }
      .addUserBtn {
        margin-bottom: 20px;
        background: #ff6f4b;
        color: white;
      }
      .deleUserBtn {
        color: #ff6f4b;
        border: 1px solid #ff6f4b;
      }
      .addUserBtn,
      .deleUserBtn {
        min-height: 25px;
        line-height: 25px;
        padding: 0 5px;
        text-align: center;
        cursor: pointer;
        user-select: none;
      }
    }
    .transfer-right {
      width: 45%;
      display: flex;
      justify-content: flex-end;
      .right-content {
        width: 100%;
        border: 1px solid rgb(224, 221, 221);
        border-radius: 5px;
        padding: 10px;
      }
    }
  }
}
</style>